/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * Description
 *
 * User: jinchen
 * Date: Jun 7, 2003
 * Time: 1:47:52 PM
 */
package edu.psu.geovista.app.pcp.plot.control.tools;

import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.layer.AxisLayer;
import edu.psu.geovista.app.pcp.plot.layer.DataLayer;
import edu.psu.geovista.app.pcp.plot.layer.DynamicLayer;
import edu.psu.geovista.app.plot.axis.XAxisOrder;
import edu.psu.geovista.app.plot.axis.YAxis;
import edu.psu.geovista.app.plot.layer.Layer;

import java.awt.event.MouseEvent;

public class AxisOrderTool extends BasicMouseTool{
     public static final boolean DEBUG = false;

   XAxisOrder xAxis;
   //int numDimensions;

    public AxisOrderTool(BasicPcpUI ui) {
        super(ui);
        this.xAxis =(XAxisOrder) getUI().getPlot().getXaxis() ;
        //numDimensions=axisList.size();

    }

    public void mousePressed(MouseEvent e) {
         this.updateLCpoint(e.getPoint()) ;

         this.setActiveAxis(findActiveAxis(cp.x,cp.y));
         /*if (! this.isToolOn() ){//set active axis only at i
            this.setActiveAxis(findActiveAxis(cp.x,cp.y));
         }*/

         if (activeAxis!=null){
             this.setToolOn(true);
         }
         //dp{
         if (DEBUG){
             System.out.println("mouse press on ");
         }//dp}
     }
     public void mouseDragged(MouseEvent e) {
         //e.isShiftDown();


         if (DEBUG){
             System.out.println("mouse drag on ");
         }//
         this.updateLCpoint(e.getPoint()) ;
         //int numDimensions=axisList.size();

         if (this.activeAxis !=null){
             if(!this.isAxisReorderable( activeAxis)){
                 return;
             }
              //draw preview axis moving
             DynamicLayer dl=(DynamicLayer)ui.getLayer(Layer.DYNAMIC_LAYER );
             dl.previewMoveAxis(activeAxis,0,cp.x);

             //switch axis
             int activeAxisIndex=activeAxis.getIndex() ;
             int newAxisIndex=this.detectToAxis(cp.x);

            /* if(DEBUG)
             System.out.println("ax:"+activeAxisIndex+" nx:"+newAxisIndex);*/
             // set movable range
             /*if (activeAxisIndex < 0) activeAxisIndex = 0;
             if (activeAxisIndex >= numDimensions) activeAxisIndex = numDimensions-1;*/
             //if (newAxisIndex < 0) newAxisIndex = 0;
             //if (newAxisIndex >= numDimensions) newAxisIndex = numDimensions-1;

             if (activeAxisIndex!=newAxisIndex){

                 YAxis toAxis=this.getAxisList().getAxis(newAxisIndex) ;
                 if(this.isAxisReorderable(toAxis) )
                     this.exchangeAxisOrder(activeAxisIndex,newAxisIndex);

                 /*
                 DataLayer dataLayer=(DataLayer)this.ui.getLayer(Layer.AllData_LAYER );
                 int[] axs=new int[]{activeAxisIndex,newAxisIndex};
                 dataLayer.repaintAxis(axs);    */
             }
             else{
                 /*if(DEBUG)
                 System.out.println("ax:"+activeAxisIndex+" nx:"+newAxisIndex);*/
             }
             //update GUI
             //ui.getPlotCanvas().repaint() ;
             this.repaintPlot() ;
              //pcp.previewAxisMove(cp.x,cp.y,activeAxis);
         }
         else{
                 System.out.println("activeAxis=null");
         }
     }





    /*******************************************************************************************************
     *               Order Axis operation
     *******************************************************************************************************/

    /**
      *
      * @param from   index of axis
      * @param to
      */
     public void exchangeAxisOrder(int from, int to) {
         AxisLayer axisLayer=(AxisLayer)this.ui.getLayer(Layer.AXIS_LAYER );
         axisLayer.switchAxisX(from,to);

         Object[] dataLys=this.getLayerByType(Layer.TYPE_DATA );
         for (int i=0;i<dataLys.length ;i++){
             DataLayer dataLayer=(DataLayer)dataLys[i];//(DataLayer)this.ui.getLayer(Layer.AllData_LAYER );
             int[] axs=new int[]{from,to};
             dataLayer.repaintAxis(axs);
         }
     }
     /**
      * Find out the next candidate axis to switch
      * @param x           the current mouse position's x
      * @return            the index of axis(toAxis) with whom the activeAxis can exchange the location.
      *                    It is determined based on x .
      */
     protected  int detectToAxis(int x){
            int activeAxisIndex=activeAxis.getIndex() ;

            int deltaX=cp.x-activeAxis.getLocation().x;//with reorder axis, only considering x
             //System.out.println("deltaX:"+deltaX+","+"cp.x: "+cp.x+"activeAxis.getLocation().x "+activeAxis.getLocation().x);
             int deltaIndex=0;
             if (deltaX>0){
                 if (activeAxisIndex==this.getNumOfAxis() -1){
                     return this.getNumOfAxis() -1;//last axis can't move right
                 }
                 //deltaIndex=deltaX/this.xAxis.getInterval(activeAxisIndex+1);
                 return xAxis.findLeftAxis(x);
             }
             else if (deltaX<0){
                 if (activeAxisIndex==0) return 0;//first axis can't move left
                /* if(this.xAxis.getInterval(activeAxisIndex)!=0)
                    deltaIndex=deltaX/this.xAxis.getInterval(activeAxisIndex);*/
                  return xAxis.findLeftAxis(x)+1;

             }
             int newAxisIndex = activeAxisIndex+deltaIndex;
             return  newAxisIndex;

     }
   /*work only if all display type is nml
   protected  int detectToAxis(int x){
            int activeAxisIndex=activeAxis.getIndex() ;

            int deltaX=cp.x-activeAxis.getLocation().x;//with reorder axis, only considering x
             System.out.println("deltaX:"+deltaX+","+"cp.x: "+cp.x+"activeAxis.getLocation().x "+activeAxis.getLocation().x);
             int deltaIndex=0;
             if (deltaX>0){
                 if (activeAxisIndex==this.getNumOfAxis() -1){
                     return this.getNumOfAxis() -1;//last axis can't move right
                 }
                 deltaIndex=deltaX/this.xAxis.getInterval(activeAxisIndex+1);

             }
             else if (deltaX<0){
                 if (activeAxisIndex==0) return 0;//first axis can't move left
                 if(this.xAxis.getInterval(activeAxisIndex)!=0)
                    deltaIndex=deltaX/this.xAxis.getInterval(activeAxisIndex);


             }
             int newAxisIndex = activeAxisIndex+deltaIndex;
             return  newAxisIndex;

     }*/
    /*******************************************************************************************************
     *                support
     *******************************************************************************************************/

     /**
      * Only YAxis with YAxis.DTYPE_Nml as display type can be reordered
      * @param axis
      * @return
      */
     private boolean isAxisReorderable(YAxis axis){
         String dtype=axis.getDisplayType() ;
         if(dtype.equals(YAxis.DTYPE_Nml ) ){
             return true;
         }
         else{
             return false;
         }

     }

     public int getNumOfAxis() {
         return getAxisList().size();
     }


}
